package com.sky.controller.admin;

import cn.hutool.core.util.IdUtil;
import com.sky.result.Result;
import com.sky.utils.AliyunOssOperator;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件上传
 *
 * @Author mr.zhang
 * Date 2025/5/20 20:57
 */
@Api(tags = "文件上传")
@RestController
@RequestMapping("/admin/common")
@Slf4j
public class FileUploadController {
   /* @Autowired
    private AliOssUtil aliOssUtil;*/
    @Autowired
    private AliyunOssOperator aliyunOssOperator;
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传:{}",file);
        try {
            byte[] bytes = file.getBytes();
            String originalFilename = file.getOriginalFilename();//获取原始文件名
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));//获取后缀名
            String url = IdUtil.fastSimpleUUID() + extension;
            String filePath = aliyunOssOperator.upload(url,bytes);
//            String filePath = aliOssUtil.upload(bytes,url);
            return Result.success(filePath);
        } catch (IOException e) {
            return Result.error(e.getMessage());
        }
    }
}
